package com.sq.partner.manager.service.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.ICoreDeptDao;
import com.sq.partner.manager.dao.IGatewayChannelInfoConfigDao;
import com.sq.partner.manager.dao.IGatewayCustomerDao;
import com.sq.partner.manager.dao.IGatewayEmailDao;
import com.sq.partner.manager.dao.IGatewayPolicyDao;
import com.sq.partner.manager.dao.IGatewaySMSDao;
import com.sq.partner.manager.dao.IGatewaySubChannelDao;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.GatewayEmail;
import com.sq.partner.manager.mybatis.model.GatewayPolicy;
import com.sq.partner.manager.mybatis.model.GatewaySMS;
import com.sq.partner.manager.mybatis.model.TreeDto;
import com.sq.partner.manager.service.IGatewayLifePolicyListService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.excel.ExcelEntity;
import com.sq.partner.manager.util.excel.ExportExcelBuilderTool;
@Service
public class GatewayLifePolicyListServiceImpl implements IGatewayLifePolicyListService {

	@Resource
	private ICoreDeptDao coreDeptDao;
	
	@Resource
	private IGatewayChannelInfoConfigDao channelInfoConfigDao;
	
	@Resource
	private IGatewaySubChannelDao subChannelConfigDao;
	
	@Resource
	private IGatewayPolicyDao policyDao;
	
	@Resource
	ExportExcelBuilderTool exportExcelBuilderTool;
	@Resource
	private IGatewayCustomerDao customerDao;
	
	 @Resource
	 private IGatewayEmailDao gatewayEmailDao;
	 @Resource
	 private IGatewaySMSDao gatewaySMSDao;
	
	@Override
	public List<CoreDept> queryPrivilegeBranch(String deptCodeStr)
			throws Exception {

		return coreDeptDao.queryDeptList(deptCodeStr);
	}

	@Override
	public Page<CoreDept> queryDeptPageByDeptAndParentDeptCode(Page<CoreDept> page,
			String parentDeptCode) throws Exception {
		return coreDeptDao.queryCoreDeptPageByParentCode(page, parentDeptCode);
	}

	@Override
	public Page<GatewayChannelInfo> queryChannelInfoPageOfLife(
			Page<GatewayChannelInfo> page) throws Exception {
		return channelInfoConfigDao.queryChannelInfoPageOfLife(page);
	}

	@Override
	public List<Map> queryChannelInfoListOfLife(GatewayChannelInfo channelInfo,String flag)throws Exception{
		return channelInfoConfigDao.queryChannelInfoList(channelInfo,flag);
	}
	@Override
	public List<Map> queryChannelInfoList(GatewayChannelInfo channelInfo,String flag)throws Exception{
		return channelInfoConfigDao.queryChannelInfoList(channelInfo,flag);
	}
	@Override
	public List<TreeDto> querySubChannelTreeByParentChannelCode(
							String parentChannelCode) throws Exception{
		return subChannelConfigDao.querySubChannelTreeByParentChannelCode(parentChannelCode);
	}

	@Override
	public Page queryLifePolicyPageByCriteria(
			Page page) throws Exception {
		
		return policyDao.queryLifePolicyPageByCriteria(page);
	}

	@Override
	public List<Map<String, Object>> queryLifePolicyListByCriteria(
			Map<String, Object> map) throws Exception {
		
		return policyDao.queryLifePolicyListByCriteria(map);
	}
	@Override
	public List<Map<String, Object>> queryLifePolicyEinvoiceListByCriteria(
			Map<String, Object> map) throws Exception {
		
		return policyDao.queryLifePolicyEinvoiceListByCriteria(map);
	}

	@Override
	public void downloadLifePolicyFile(List<Map<String, Object>> lifePolicyList,HttpServletResponse response)
			throws Exception {
		ExcelEntity excel = new ExcelEntity();
		excel.setResults(lifePolicyList);
		excel.setFileName("LifePolicyInfo");
		excel.setTitles("保单号,投保人,投保单位,投保单位子机构,子机构,投保方案,人数,保费,保单提交日期,起保日期,终保日期,单证号,状态,行程/航班,经办人,经办人电话,用户名,姓名,第三方经办人");
		excel.setTitleKeys("POLNO,TBR,CHANNEL_NAME,SUB_CHANNEL_NAME,PARTNER_SUB_CODE,INSU_PLAN_NAME,BBR_NUM,PREMIUM,INSERT_TIME,BEGIN_DATE,END_DATE,CARD_NO,STATE_ID,APP_CONTENT,PURVEYOR_NAME,PURVEYOR_TEL,USER_CODE,USER_NAME,THIRD_AGENT");
		excel.setColumns("35,15,10,25,25,30,15,15,10,15,23,23,23,25,10,15,15,15,15");

		exportExcelBuilderTool.buildExcel(response,excel);		
	}

	@Override
	public GatewayPolicy queryPolicyInfoByPolNo(String polNo) throws Exception {
		return policyDao.queryPolicyInfoByPolNo(polNo);
	}

	@Override
	public void downloadLifeTeamPolicyBBRInfoList(String polNo, HttpServletResponse response) throws Exception {
		List<Map<String, Object>> bbrls = customerDao.queryBBRInfoForTeamByPolNo(polNo);
		ExcelEntity excel = new ExcelEntity();
		excel.setFileName("被保险人清单");
		excel.setTitles("*姓名,*证件类型,*证件号码,出生日期,性别,联系人电话");
		excel.setTitleKeys("REAL_NAME,CERTI_TYPE_DSCP,CERTI_CODE,BIRTHDAY_STR,GENDER,MOBILE");
		excel.setResults(bbrls);
		excel.setColumns("15,20,26,15,10,20");
		excel.setTotalTitle("被保险人清单");
		exportExcelBuilderTool.buildExcel(response,excel);
	}

	@Override
	public Page queryLifePolicyEinvoiceInfoByPage(Page page) throws SQLException {
		
		return policyDao.queryLifePolicyEinvoiceInfoByPage(page);
	}
	/**
	 * 查询总量
	 */
	@Override
	public List<Map<String, String>> queryLifePolicyAll(Map<String, String> map) throws Exception { 
	    return policyDao.queryLifePolicyAll(map);
	}
	/**
     * 导出Excel
     */
    @Override
    public void downloadLifePolicyFile1(List<Map<String, Object>> lifePolicyList,HttpServletResponse response)
            throws Exception {
        ExcelEntity excel = new ExcelEntity();
        excel.setResults(lifePolicyList);
        excel.setFileName("LifePolicyInfo");
        // excel.setTitles("保单号,投保人,投保单位,投保单位子机构,子机构,投保方案,人数,保费,保单提交日期,起保日期,终保日期,单证号,状态,行程/航班,经办人,经办人电话,用户名,姓名,第三方经办人");
        excel.setTitles("保单号,交易流水号,投保人,投保单位,合作伙伴代码,投保单位子机构,子机构/网点,出单机构,投保方案,人数,保费,缴费日期,保单提交日期,起保日期,终保日期,单证号,状态,行程/航班");
        
        // excel.setTitleKeys("POLNO,TBR,CHANNEL_NAME,SUB_CHANNEL_NAME,PARTNER_SUB_CODE,INSU_PLAN_NAME,BBR_NUM,PREMIUM,INSERT_TIME,BEGIN_DATE,END_DATE,CARD_NO,STATE_ID,APP_CONTENT,PURVEYOR_NAME,PURVEYOR_TEL,USER_CODE,USER_NAME,THIRD_AGENT");
        excel.setTitleKeys("POLNO,CHANNEL_SEQNO,TBR,CHANNEL_NAME,CHANNEL_CODE,SUB_CHANNEL_NAME,PARTNER_SUB_CODE,DEPT_CODE,PLAN_NAME,BBR_NUM,PREMIUM,TRXDATE,INSERT_TIME,BEGIN_DATE,END_DATE,CARD_NO,STATE_ID,APP_CONTENT");
        excel.setColumns("35,25,15,10,25,25,25,25,30,15,15,15,10,15,23,23,23,25");

        exportExcelBuilderTool.buildExcel(response,excel);      
    }
    /**
     * 导出Excel 界面出单
     */
    @Override
    public void downloadLifeFacadePolicyFileByCriteria(List<Map<String, Object>> lifePolicyList,HttpServletResponse response)
            throws Exception {
        ExcelEntity excel = new ExcelEntity();
        excel.setResults(lifePolicyList);
        excel.setFileName("LifePolicyInfo");
        excel.setTitles("保单号,投保人,投保单位,投保单位子机构,子机构/网点,投保方案,是否见费,人数,保费,保单提交日期,起保日期,终保日期,单证号,状态,行程/航班,经办人,经办人电话,用户名,姓名,第三方经办人");
        
        excel.setTitleKeys("POLNO,TBR,CHANNEL_NAME,SUB_CHANNEL_NAME,PARTNER_SUB_CODE,PLAN_NAME,JF_FLAG,BBR_NUM,PREMIUM,INSERT_TIME,BEGIN_DATE,END_DATE,CARD_NO,STATE_ID,APP_CONTENT,PURVEYOR_NAME,PURVEYOR_TEL,USER_CODE,USER_NAME,THIRD_AGENT");
        excel.setColumns("35,15,10,25,25,30,30,15,15,10,15,23,23,23,25,10,15,15,15,15");

        exportExcelBuilderTool.buildExcel(response,excel);      
    }
    
    /**
     * 界面出单导出Excel
     */
    @Override
     public List<Map<String, Object>> queryLifeFacadePolicyListByCriteria(Map<String, Object> map) throws Exception {
         return policyDao.queryLifeFacadePolicyListByCriteria(map);
     }
    
    @Override
	public Result addEmailReSend(String polNo, String email) throws Exception {
		/**查询已发送过的邮件*/
		List<GatewayEmail> emails=gatewayEmailDao.queryEmailByPolNo(polNo);
		if(emails==null||emails.size()==0){
			return Result.error("此单没有历史邮件发送记录");
		}
		GatewayEmail emailDto=emails.get(0);
		emailDto.setEmail(email);
		if(emailDto.getEmailId().contains("CF")){
			emailDto.setEmailId("CF"+(Integer.parseInt(emailDto.getEmailId().replace("CF", ""))+emails.size()));
		}else{
			emailDto.setEmailId("CF"+emailDto.getEmailId()+emails.size());
		}
		
		if(gatewayEmailDao.addGateWayEmail(emailDto)){
			return Result.success("发送成功");
		};
		return Result.error("发送失败");
	}

	@Override
	public Result addSMSReSend(String polNo, String phoneNo) throws Exception {
		SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format1=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		/**查询已发送过的短信*/
		List<GatewaySMS> emss=gatewaySMSDao.querySMSByPolNo(polNo);
		if(emss==null||emss.size()==0){
			return Result.error("此单没有历史短信发送记录");
		}
		GatewaySMS ems=emss.get(0);
		if(ems.getSmsId().contains("CF")){
			ems.setSmsId("CF"+(Integer.parseInt(ems.getSmsId().replace("CF", ""))+emss.size()));
		}else{
			ems.setSmsId("CF"+ems.getSmsId()+emss.size());
		}
		ems.setPhoneCode(phoneNo);
		ems.setSendTime(format.format(new Date()));
		ems.setId("CF"+format1.format(new Date())+"560709");
		if(gatewaySMSDao.addGateWaySMS(ems)){
			return Result.success("发送成功");
		};
		return Result.error("发送失败");
	}

	@Override
	public String querySingleEndorseByPolno(String polno) throws Exception {
		
		return policyDao.querySingleEndorseByPolno(polno);
	}

}
